<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
		<title>Test EK.modelLocator</title>
        <script type="text/javascript" src="../../../jsunit/app/jsUnitCore.js"></script>
        <script type="text/javascript" src="../../../jsunit/app/jsUnitMockTimeout.js"></script>
		<script type="text/javascript" src="../src/ek.js"></script>
		<script type="text/javascript" src="../src/ek.eventable.js"></script>		
		<script type="text/javascript" src="../src/ek.model-locator.js"></script>
	</head>
	<body>
		<script type="text/javascript">
			var obj, arry;
			var twoLevelTestArray, FlatTestObject, TwoLevelTestObject, ThreeLevelTestObject;

			function setUp() {
				twoLevelTestArray = [{prop1:'blah1'}, {prop2:'blah2'}];
	
				FlatTestObject = function () {
					this.prop = 'blah';
				};
				
	            TwoLevelTestObject = function () {
	                this.prop = { innerProp: 'innerBlah' }
	            };
	
	            ThreeLevelTestObject = function () {
	                this.prop = { innerProp: { innerInnerProp: 'innerInnerBlah' } }
	            };
			}

			function testChangeOnFlatObject() {
				var propChanged = undefined;
                Clock.reset();
				obj = EK.modelLocator.x('FlatTestObject');
				obj.bind('change', function (prop) { propChanged = prop; });
                assertUndefined(propChanged);
				obj.prop = 'changed';
                Clock.tick(25);
                assertEquals('prop', propChanged);
			}
            
			function testChangeOnTwoLevelObject() {
				var propChanged = undefined;
                Clock.reset();
				obj = EK.modelLocator.x('TwoLevelTestObject');
				obj.bind('change', function(prop){ propChanged = prop; });
                assertUndefined(propChanged);
				obj.prop.innerProp = 'changed';
                Clock.tick(25);
                assertEquals('innerProp', propChanged);
			}

			function testChangeOnThreeLevelObject() {
				var propChanged = undefined;
                Clock.reset();
				obj = EK.modelLocator.x('ThreeLevelTestObject');
				obj.bind('change', function (prop) { propChanged = prop; });
                assertUndefined(propChanged);
				obj.prop.innerProp.innerInnerProp = 'changed';
                Clock.tick(25);
                assertEquals('innerInnerProp', propChanged);
			}
			
			function testChangeOnTwoLevelArray() {
				var propChanged = undefined;
                Clock.reset();
				obj = EK.modelLocator.x('twoLevelTestArray');
				obj.bind('change', function (prop) { propChanged = prop; });
                assertUndefined(propChanged);
				obj[0].prop1 = 'changed';
                Clock.tick(25);
                assertEquals('prop1', propChanged);
			}
			
			function testChangeOnAddToFlatObject() {
				var propChanged = undefined;
                Clock.reset();
				obj = EK.modelLocator.x('FlatTestObject');
				obj.bind('change', function (prop) { propChanged = prop });
				obj.addedProp = 'added';
                assertUndefined(propChanged);
				obj.prop = 'changed';
                Clock.tick(25);
				assertEquals('prop', propChanged);
				obj.addedProp = 'changed';
                Clock.tick(25);
                assertEquals('addedProp', propChanged); 				
			}
			
			function testChangeOnAddToTwoLevelObject() {
				var propChanged = undefined;
                Clock.reset();
				obj = EK.modelLocator.x('TwoLevelTestObject');
				obj.bind('change', function (prop) { propChanged = prop });
				obj.prop.addedInnerProp = 'added';
                assertUndefined(propChanged);
				obj.prop.innerProp = 'changed';
                Clock.tick(25);
				assertEquals('innerProp', propChanged);
				obj.prop.addedInnerProp = 'changed';
                Clock.tick(25);
                assertEquals('addedInnerProp', propChanged); 				
			}

			function testChangeOnReplaceToTwoLevelObject() {
				var propChanged = undefined;
                Clock.reset();
				obj = EK.modelLocator.x('TwoLevelTestObject');
				obj.bind('change', function (prop) { propChanged = prop });
				assertUndefined(propChanged);
				obj.prop.innerProp = {replaceProp:'replaced'};;
                Clock.tick(25);
                assertEquals('innerProp', propChanged);
				propChanged = undefined;
				assertUndefined(propChanged);
				obj.prop.innerProp.replaceProp = 'changed';
                Clock.tick(25);
                assertEquals('innerProp', propChanged); 				
			}
			
			function testChangeOnReplaceToTwoLevelArray() {
				var propChanged = undefined;
                Clock.reset();
				obj = EK.modelLocator.x('twoLevelTestArray');
				obj.bind('change', function (prop) { propChanged = prop });
				assertUndefined(propChanged);
				obj[0].prop1 = {replaceProp:'replaced'};
                Clock.tick(25);
                assertEquals('prop1', propChanged);
				propChanged = undefined;
				assertUndefined(propChanged);
				obj[0].prop1.replaceProp = 'changed';
                Clock.tick(25);
				assertEquals('prop1', propChanged);
			}
		</script>
	</body>
</html>
